package com.lumacong.websocket.controller;

import com.alibaba.fastjson.JSON;
import com.lumacong.websocket.server.DsGamesDayServer;
import com.lumacong.websocket.service.DsGamesDataDayService;
import com.lumacong.websocket.service.DsGamesDayService;
import com.lumacong.websocket.vo.DsGamesDay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;

@RestController
@RequestMapping("/push_socket")
public class SocketController {
    @Autowired
    DsGamesDayService dsGamesDayService;

    @Autowired
    DsGamesDataDayService dsGamesDataDayService;

    /**
     * http://localhost:20008/push_socket/initDsGamesDayTask
     */
    @GetMapping("initDsGamesDayTask")
    public void initDsGamesDayTask() {
        System.out.println("-----initDsGamesDayTask-------");
        CopyOnWriteArraySet<DsGamesDayServer> webSocketSet = DsGamesDayServer.getWebSocketSet();

        webSocketSet.forEach(c -> {
            try {
                List<DsGamesDay> dsGamesDays = dsGamesDayService.initDsGamesDay();
                if (dsGamesDays != null) {
                    String msg = JSON.toJSONString(dsGamesDays);
                    c.sendMessage(msg);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

}
